package org.br.erp.base.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.br.erp.base.entity.Scmobject;
import org.br.erp.base.entity.Scmprintdefinition;
import org.br.erp.base.entity.Scmprintdefrelation;
import org.br.erp.base.entity.Scmprintformat;
import org.br.erp.base.mapper.ScmobjectMapper;
import org.br.erp.base.mapper.ScmprintdefinitionMapper;
import org.br.erp.base.mapper.ScmprintdefrelationMapper;
import org.br.erp.base.mapper.ScmprintformatMapper;
import org.br.erp.base.service.IScmprintformatService;
import org.br.erp.base.utils.ERPUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 打印数据源
 * @Author: jeecg-boot
 * @Date:   2022-12-18
 * @Version: V1.0
 */
@Service
public class ScmprintformatServiceImpl extends ServiceImpl<ScmprintformatMapper, Scmprintformat> implements IScmprintformatService {

    @Autowired
    ScmprintformatMapper scmprintformatMapper;

    @Autowired
    ScmprintdefinitionMapper scmprintdefinitionMapper;

    @Autowired
    ScmprintdefrelationMapper scmprintdefrelationMapper;

    @Autowired
    ScmobjectMapper scmobjectMapper;


    @Override
    @Transactional
    public void savefomrat(Scmprintformat scmprintformat) {
//        //先删除当前业务对象所属的打印格式
//        String objectcode = scmprintformat.getObjectcode();
//        QueryWrapper<Scmprintformat> queryWrapper=new QueryWrapper<>();
//        queryWrapper.eq("objectcode",objectcode);
//        scmprintformatMapper.delete(queryWrapper);

        //
        QueryWrapper<Scmprintformat> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("printdefid",scmprintformat.getPrintdefid());
        scmprintformatMapper.delete(queryWrapper);

        Scmprintdefinition scmprintdefinition = scmprintdefinitionMapper.selectById(scmprintformat.getPrintdefid());
        if(scmprintdefinition!=null){
            Scmobject scmobject = scmobjectMapper.selectById(scmprintdefinition.getObjectid());
            if(scmobject!=null){
                scmprintformat.setObjectcode(scmobject.getObjectcode());
                scmprintformat.setObjectname(scmobject.getObjectname());
            }
        }
        scmprintformatMapper.insert(scmprintformat);
    }

    @Override
    public Scmprintformat queryPrintFormatByObjectCode(String objectCode) {
        QueryWrapper<Scmprintformat> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("objectcode",objectCode);
        List<Scmprintformat> scmprintformats = scmprintformatMapper.selectList(queryWrapper);
        if(scmprintformats!=null && scmprintformats.size()>0){
            return scmprintformats.get(0);
        }else {
            return null;
        }
    }

    @Override
    public Scmprintformat queryPrintFormatByPrintdefid(String printdefid) {
        QueryWrapper<Scmprintformat> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("printdefid",printdefid);
        List<Scmprintformat> scmprintformats = scmprintformatMapper.selectList(queryWrapper);
        if(scmprintformats!=null && scmprintformats.size()>0){
            return scmprintformats.get(0);
        }else {
            //如果是第一次则将系统格式加载上
            Scmprintdefinition scmprintdefinition = scmprintdefinitionMapper.selectById(printdefid);
            if(scmprintdefinition!=null){
                Scmobject scmobject = scmobjectMapper.selectById(scmprintdefinition.getObjectid());
                if(scmobject!=null){
                    String objectcode = scmobject.getObjectcode();
                    QueryWrapper<Scmprintformat> queryWrapper1 =  new QueryWrapper<>();
                    queryWrapper1.eq("objectcode",objectcode);
                    queryWrapper1.and(item->item.isNull("printdefid").or(item2->item2.eq("printdefid","")));
                    List<Scmprintformat> scmprintformats1 = scmprintformatMapper.selectList(queryWrapper1);
                    if(scmprintformats1!=null && scmprintformats1.size()>0){
                        return scmprintformats1.get(0);
                    }
                }else{
                    return null;
                }
            }else {
                return null;
            }
        }
        return null;
    }

    @Override
    public Scmprintformat queryPrintFormatByComanyOrUser(Map reqMap) {

        JSONArray jsonArray=new JSONArray();

        //先去根据打印格式分配中查询所属的打印格式定义

        //先去按照 公司和人去查找
        QueryWrapper<Scmprintdefrelation> queryWrapper=new QueryWrapper<>();

        if(ERPUtils.ifHtppReqParamContainKey(reqMap,"objectcode")){
            String objectcode = ERPUtils.getHttpReqParam(reqMap, "objectcode");
            queryWrapper.eq("objectcode",objectcode);
        }
        if(ERPUtils.ifHtppReqParamContainKey(reqMap,"companyid")){
            queryWrapper.eq("companyid",ERPUtils.getHttpReqParam(reqMap,"companyid"));
        }

       LoginUser loginUser= (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(loginUser!=null){
            queryWrapper.eq("printuserid",loginUser.getId());
        }

        String ids = ERPUtils.getHttpReqParam(reqMap, "ids");

        String[] split = ids.split(",");
        String printdefid="";
        List<Scmprintdefrelation> scmprintdefrelations = scmprintdefrelationMapper.selectList(queryWrapper);
        if(scmprintdefrelations!=null && scmprintdefrelations.size()>0){
            Scmprintdefrelation scmprintdefrelation = scmprintdefrelations.get(0);
            printdefid= scmprintdefrelation.getPrintdefid();
        }
        else{
            //为空，再用其他条件查找
            queryWrapper.clear();
            queryWrapper.eq("objectcode",ERPUtils.getHttpReqParam(reqMap, "objectcode"));
            queryWrapper.and(item->item.isNull("companyid").or(item2->item2.eq("companyid","")));
            queryWrapper.eq("printuserid",loginUser.getId());
            List<Scmprintdefrelation> newprintdefrelation = scmprintdefrelationMapper.selectList(queryWrapper);
            if(newprintdefrelation!=null && newprintdefrelation.size()>0){
                printdefid=newprintdefrelation.get(0).getPrintdefid();
            }else{
                queryWrapper.clear();
                queryWrapper.eq("objectcode",ERPUtils.getHttpReqParam(reqMap, "objectcode"));
                queryWrapper.and(item->item.isNull("printuserid").or(item2->item2.eq("printuserid","")));
                queryWrapper.eq("companyid",ERPUtils.getHttpReqParam(reqMap,"companyid"));
                List<Scmprintdefrelation> new2printdefrelation = scmprintdefrelationMapper.selectList(queryWrapper);
                if(new2printdefrelation!=null && new2printdefrelation.size()>0){
                    printdefid=new2printdefrelation.get(0).getPrintdefid();
                }else{
                    throw  new RuntimeException("当前未定义格式！");
                }
            }
        }
        if(printdefid.equals("")){
            throw new RuntimeException("格式未定义，请检查格式分配！");
        }

        QueryWrapper<Scmprintformat> scmprintformatQueryWrapper=new QueryWrapper<>();
        scmprintformatQueryWrapper.eq("printdefid",printdefid);
        List<Scmprintformat> scmprintformats = scmprintformatMapper.selectList(scmprintformatQueryWrapper);
        if(scmprintformats!=null && scmprintformats.size()>0){
            return scmprintformats.get(0);
        }else{
            throw new RuntimeException("当前未查找到格式");
        }

    }
}
